package graph.drawingshapes
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	
	import mx.events.PropertyChangeEvent;

	public class DrawingShapeBase extends Sprite
	{
		public function DrawingShapeBase()
		{
			super();
			initHandlers();
		}
		
		protected virtual function initHandlers():void
		{
			this.addEventListener(RedrawEvent.REDRAW, onRedrawEvent);
		}
		
		protected virtual function onRedrawEvent(event:RedrawEvent):void
		{
			clearGraphics();
			initDrawing();
			draw();
			endDrawing();
		}
		
		protected function clearGraphics():void
		{
			this.graphics.clear();
		}
		
		protected virtual function initDrawing():void{}
				
		protected virtual function draw():void{}
		
		protected virtual function endDrawing():void{}
		
		public virtual function getBitmapData():BitmapData
		{
			var result:BitmapData = new BitmapData(this.width, this.height,true,0x000000ff);
			result.draw(this);
			return result;
		}
		
		protected virtual function firePropertyChanged(property:String, oldValue:Object, newValue:Object):void
		{
			if (!this.hasOwnProperty(property)) return;
			var event:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, property, oldValue, newValue);
			dispatchEvent(event);			
		}
	
	}
}